package com.bwie.service.impl;

import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beust.jcommander.Strings;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.GroupActive;
import com.bwie.utils.R;
import com.bwie.utils.TokenUtil;
import com.bwie.vo.PageInfoVo;
import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import jdk.nashorn.internal.parser.Token;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;
import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.GroupJoin;
import com.bwie.mapper.GroupJoinMapper;
import com.bwie.service.GroupJoinService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: LiuWeiChen
 * @Date:  2025/7/26 11:58
 * @Description: 
 **/
@Transactional
@Service
public class GroupJoinServiceImpl extends ServiceImpl<GroupJoinMapper, GroupJoin> implements GroupJoinService{
    @Autowired
    private GroupJoinMapper groupJoinMapper;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private FastFileStorageClient fileStorageClient;


    @Override
    public R joinActiveList(PageInfoVo pageInfoVo) {
        Page<GroupJoin> page = new Page<>(pageInfoVo.getPageNum(), pageInfoVo.getPageSize());
        Page<GroupJoin> groupJoinList = groupJoinMapper.joinActiveList(page,pageInfoVo);
        List<GroupJoin> records = groupJoinList.getRecords();
        for (GroupJoin record : records) {
            String joinImages = record.getJoinImages();
            if (StringUtils.isNotEmpty(joinImages)){
                List<String> prodImages = JSON.parseArray(joinImages, String.class);
                record.setProdImages(prodImages);
            }
        }
        return R.OK(groupJoinList);
    }

    @Override
    public R updateJoinActive(GroupJoin groupJoin, BindingResult result) {
        //检测是否存在
        GroupJoin one = groupJoinMapper.selectById(groupJoin.getActiveId());
        if (ObjectUtil.isEmpty(one)){
            return R.ERROR(500,"该活动已不存在");
        }
        groupJoin.setJoinImages(JSON.toJSONString(groupJoin.getProdImages()));
        String token = request.getHeader("token");
        String userName = TokenUtil.getUserName(token);
        groupJoin.setUpdateBy(userName);
        groupJoin.setUpdateTime(new Date());
        groupJoinMapper.updateById(groupJoin);
        return R.OK();
    }

    @Override
    public R uploadFile(MultipartFile file) {
        String extName = FileNameUtil.extName(file.getOriginalFilename());
        try {
            StorePath storePath = fileStorageClient.uploadFile(file.getInputStream(), file.getSize(), extName,null);
            String url = "http://192.168.41.192:8888/"+storePath.getFullPath();
            return R.OK(url);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public R addGroupJoin(GroupJoin groupJoin, BindingResult result) {
        //校验活动名称
        GroupJoin one = groupJoinMapper.selectOne(new LambdaQueryWrapper<GroupJoin>().eq(GroupJoin::getJoinName, groupJoin.getGroupNum()));
        if (ObjectUtil.isNotEmpty(one)){
            return R.ERROR(500,"该活动名称已经存在,不允许添加");
        }
        groupJoin.setJoinImages(JSON.toJSONString(groupJoin.getProdImages()));
        String userName = TokenUtil.getUserName(request.getHeader("token"));
        groupJoin.setCreateBy(userName);
        groupJoin.setCreateTime(new Date());
        //添加活动
        groupJoinMapper.insert(groupJoin);
        return R.OK();
    }

    @Override
    public R deleteGroupJoin(GroupJoin groupJoin) {
        GroupJoin one = groupJoinMapper.selectById(groupJoin.getActiveId());
        if (ObjectUtil.isEmpty(one)){
            return R.ERROR(500,"改拼团活动已不存在");
        }
        //删除
        groupJoinMapper.deleteById(groupJoin);
        return R.OK();
    }
}
